/**
 *@Title:ContactUtil.java
 *@Package:com.renmai.contact
 *@Description:TODO
 *@Author:Issac
 *@Date:2011-7-2下午04:49:29
 *@Version:v1.0
 */
package com.renmai.contact;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.Contacts.People;

/**
 *@ClassName:ContactUtil
 *@Description:TODO
 *@Author:Issac
 *@Date:2011-7-2下午04:49:29 
 */
public class ContactUtil extends Activity {
	private ArrayList<SamContact> getAllContacts()  
    {  
        ArrayList<SamContact> arrayList = new ArrayList<SamContact>();  
        //获取本机联系人  
        Cursor cur = getContentResolver().query(    
                ContactsContract.Contacts.CONTENT_URI,    
                null ,    
                null ,    
                null ,    
                ContactsContract.Contacts.DISPLAY_NAME    
                        + " COLLATE LOCALIZED ASC" );  
        if(cur.moveToFirst())  
        {  
            do{  
                SamContact samContact = new SamContact();  
                samContact.isChoosed = false;  
                int  idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);    
                int  displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
                // 获得联系人的ID号     
                String contactId = cur.getString(idColumn);    
                // 获得联系人姓名     
                String disPlayName = cur.getString(displayNameColumn);  
                System.out.println(disPlayName);  
                samContact.name = disPlayName;  
                // 查看该联系人有多少个电话号码。如果没有这返回值为0     
                int  phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
                if(phoneCount <1)  
                {  
                    continue;  
                }  
                Cursor phones = getContentResolver().query(    
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,    
                        null ,    
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID    
                                + " = "  + contactId,  null ,  null );  
                if  (phones.moveToFirst())   
                {    
                    do  {    
                        // 遍历所有的电话号码     
                        String phoneNumber = phones    
                                .getString(phones    
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));    
                        int phoneType = phones    
                                .getInt(phones    
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));  
                        if(phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)  
                        {  
                            samContact.phone = phoneNumber;  
                            arrayList.add(samContact);  
                            break;  
                        }  
                    } while  (phones.moveToNext());   
                      
                }  
                  
                  
                  
            }while(cur.moveToNext());  
        }  
        //获取sim卡联系人  
        Uri uri = Uri.parse("content://icc/adn");  
        Cursor cur2 = getContentResolver().query(    
                uri,    
                null ,    
                null ,    
                null ,    
                ContactsContract.Contacts.DISPLAY_NAME    
                        + " COLLATE LOCALIZED ASC" );  
        System.out.println("contact num from sim card = "+cur2.getCount());  
        System.out.println("---------------");  
        if(cur2.moveToFirst())  
        {  
            do{  
                try   
                {  
                    SamContact samContact = new SamContact();  
                    samContact.isChoosed = false;  
                    int  displayNameColumn = cur2.getColumnIndex(People.NAME);  
                    int  phoneColumn = cur2.getColumnIndex(People.NUMBER);  
                    samContact.name  = cur2.getString(displayNameColumn);  
                    if(samContact.name == null)  
                    {  
                        continue;  
                    }  
                    samContact.phone = cur2.getString(phoneColumn);   
                    if(samContact.phone == null)  
                    {  
                        continue;  
                    }  
                    arrayList.add(samContact);  
                }   
                catch (Exception e)   
                {  
                    e.printStackTrace();  
                }   
            }while(cur2.moveToNext());  
        }  
          
          
        return arrayList;  
    }  
      
      
    public static class SamContact   
    {  
        public String name = "";  
        public String phone = "";  
        public boolean isChoosed = false;  
          
    }  
}
